package com.atguigu.ggkt.vod.controller;


import com.atguigu.ggkt.model.vod.Chapter;
import com.atguigu.ggkt.result.Result;
import com.atguigu.ggkt.vo.vod.ChapterVo;
import com.atguigu.ggkt.vod.service.ChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Jetwang
 * @since 2022-06-30
 */
@RestController
@RequestMapping("/admin/vod/chapter")
@Api(tags = "章节管理接口")
public class ChapterController {
    @Autowired
    private ChapterService chapterService;

    /**
     * 获取大纲列表
     * @param courseId 课程id
     * @return
     */
    @ApiOperation("获取大纲列表")
    @GetMapping("chapters/{courseId}")
    public Result listChapters(@PathVariable Long courseId) {
        List<ChapterVo> list = chapterService.getTreeList(courseId);
        return Result.ok(list);
    }

    /**
     * 新增章节
     * @param chapter json格式的chapter对象
     * @return
     */
    @ApiOperation("新增章节")
    @PostMapping("chapter")
    public Result save(@RequestBody Chapter chapter) {
        chapterService.save(chapter);
        return Result.ok();
    }

    /**
     * 根据id获取章节信
     * @param id 章节id
     * @return
     */
    @ApiOperation("根据id获取章节")
    @GetMapping("chapter/{id}")
    public Result update(@PathVariable Long id) {
        Chapter chapter = chapterService.getById(id);
        return Result.ok(chapter);
    }


    /**
     * 修改章节
     * @param chapter json格式的chapter对象
     * @return
     */
    @ApiOperation("修改章节")
    @PutMapping("chapter")
    public Result update(@RequestBody Chapter chapter) {
       chapterService.updateById(chapter);
       return Result.ok();
    }

    /**
     * 根据id删除章节
     * @param id 章节id
     * @return
     */
    @ApiOperation("根据id删除章节")
    @DeleteMapping("chapter/{id}")
    public Result delete(@PathVariable Long id) {
        chapterService.deleteChapterAndVideo(id);
        return Result.ok();
    }

}

